import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Job, JobParam } from './model';

/**
 * 分页查询
 */
export async function pageJobs(params: JobParam) {
  const res = await request.get<ApiResult<PageResult<Job>>>(
    '/employ/jobs/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加
 */
export async function addJob(data: Job) {
  const res = await request.post<ApiResult<unknown>>('/employ/jobs', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改
 */
export async function updateJob(data: Job) {
  const res = await request.put<ApiResult<unknown>>('/employ/jobs', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function removeJob(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/employ/jobs/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除
 */
export async function removeJobBatch(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/employ/jobs/batch',
    {
      data
    });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改状态
 */
export async function updateJobStatus(jobId?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>('/employ/jobs/status', {
    jobId,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
